package com.rock.code.common.util;


import com.rock.code.common.util.snmp.IfEntry;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SnmpClientV2c extends SnmpClient {

    @Override
    public SnmpClient connect() throws IOException {
        Address targetAddress = GenericAddress.parse("udp:" + host + "/" + port);

        TransportMapping transport = new DefaultUdpTransportMapping();
        this.snmp = new Snmp(transport);
        transport.listen();

        CommunityTarget communitytarget = new CommunityTarget();
        communitytarget.setCommunity(new OctetString(community));
        communitytarget.setAddress(targetAddress);
        // 通信不成功时的重试次数
        communitytarget.setRetries(1);
        // 超时时间
        communitytarget.setTimeout(3000);
        communitytarget.setVersion(version);
        this.target = communitytarget;
        return this;
    }

    @Override
    protected PDU createPDU() {
        return createPDU(PDU.GET);
    }

    @Override
    protected PDU createPDU(int type) {
        PDU pdu = new PDU();
        pdu.setType(type);
        return pdu;
    }

	public static void main(String[] args) throws Exception {
		SnmpClient snmp = SnmpClient.create("v2c")
				.setHost("10.211.55.3")
				.setPort("161")
				.setCommunity("public");

		try {
			snmp.connect();
			Map<String, String> map = snmp.walk("1.3.6.1.2.1.4.22.1.2");
			List<IfEntry> list =snmp.walkIfTable("1","2");
			Map map2= snmp.walkIpAddrTable("1","2");
			System.out.println(map2);
		} finally {
			snmp.close();
		}

	}

//    public static void main(String[] args) throws Exception {
//        List<HashMap> snmpKsys = new ArrayList<HashMap>();
//        HashMap snmpKsy1 = new HashMap();
//        HashMap snmpKsy2 = new HashMap();
//        snmpKsy2.put("SNMP_VERSIONS","3");
//        snmpKsy2.put("PROT_NUM","161");
//        snmpKsy2.put("SECURITY_LEVEL","authNoPriv");
//        snmpKsy2.put("AUTHENTICATION_PROTOCOL","MD5");
//        snmpKsy2.put("AUTHENTICATION_PASSWORD","md5@jbdl");
//        snmpKsy2.put("SNMP_USER_NAME","jbdlnewuser");
//        snmpKsys.add(snmpKsy2);
//        snmpKsy1.put("SNMP_VERSIONS","2c");
//        snmpKsy1.put("READ_ONLY_GROUP_NAME","public");
//        snmpKsy1.put("PROT_NUM","161");
//        snmpKsys.add(snmpKsy1);
//        System.out.println(snmpKsys);
//        saveReadFileDataService("127.0.0.1",snmpKsys,"12222");
//    }


    private static void saveReadFileDataService(String ip, List<HashMap> snmpKeys, String scId) {
        //save tb_cmdb_ifentry(数据iftype,Ifdescr，ifPhysAddress)
        if (snmpKeys != null && snmpKeys.size() != 0) {
            for (int i = 0; i < snmpKeys.size(); i++) {
                HashMap snmpKey = snmpKeys.get(i);
                String version = snmpKey.get("SNMP_VERSIONS") == null ? "" : snmpKey.get("SNMP_VERSIONS").toString();
                String prot = snmpKey.get("PROT_NUM") == null ? "" : snmpKey.get("PROT_NUM").toString();
                String readOnlyGroupName = snmpKey.get("READ_ONLY_GROUP_NAME") == null ? "" : snmpKey.get("READ_ONLY_GROUP_NAME").toString();
                String securityLevel = snmpKey.get("SECURITY_LEVEL") == null ? "" : snmpKey.get("SECURITY_LEVEL").toString();
                String authenticationProtocol = snmpKey.get("AUTHENTICATION_PROTOCOL") == null ? "" : snmpKey.get("AUTHENTICATION_PROTOCOL").toString();
                String authenticationPassword = snmpKey.get("AUTHENTICATION_PASSWORD") == null ? "" : snmpKey.get("AUTHENTICATION_PASSWORD").toString();
                String encryptionProtocol = snmpKey.get("ENCRYPTION_PROTOCOL") == null ? "" : snmpKey.get("ENCRYPTION_PROTOCOL").toString();
                String encryptionPassword = snmpKey.get("ENCRYPTION_PASSWORD") == null ? "" : snmpKey.get("ENCRYPTION_PASSWORD").toString();
                String serurityName = snmpKey.get("SNMP_USER_NAME") == null ? "" : snmpKey.get("SNMP_USER_NAME").toString();

                if ("2c".equals(version)) {
                    SnmpClient snmp = SnmpClient.create("v2c")
                            .setHost(ip)
                            .setPort(prot)
                            .setCommunity(readOnlyGroupName);
                    try {
                        snmp.connect();
                        List<IfEntry> list = snmp.walkIfTable(ip, scId);
                        if (list != null && list.size() != 0) {
                            //保存ifentry
//                            netWorkFindDao.ifentry(list);
                            Map map = snmp.walkIpAddrTable(ip, scId);
                            //保存ipNetToMediaPhysAddress
                            List ipNetToMediaPhysAddressList = (List) map.get("ipNetToMediaPhysAddressList");
                            if (ipNetToMediaPhysAddressList != null && ipNetToMediaPhysAddressList.size() != 0) {
//                                netWorkFindDao.ipNetToMediaPhysAddressSave(ipNetToMediaPhysAddressList);
                            }
                            List ipNetToMediaTypeList = (List) map.get("ipNetToMediaTypeList");
                            if (ipNetToMediaTypeList != null && ipNetToMediaTypeList.size() != 0) {
//                                netWorkFindDao.ipNetToMediaTypeSave(ipNetToMediaTypeList);
                            }
                            List ipAddrTableList = (List) map.get("ipAddrTableList");
                            if (ipAddrTableList != null && ipAddrTableList.size() != 0) {
//                                netWorkFindDao.ipAddrTableSave(ipAddrTableList);
                            }
                            break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        snmp.close();
                    }
                }
                if ("3".equals(version)) {
                    SnmpClient snmp = null;
                    if ("authNoPriv".equalsIgnoreCase(securityLevel)) {
                        snmp = SnmpClient.create("3")
                                .setHost(ip)
                                .setPort(prot)
                                .setSerurityName(serurityName)
                                .setAuthenticationProtocol(authenticationProtocol)
                                .setAuthenticationPassphrase(authenticationPassword)
                                .setSecurityLevel(securityLevel);
                    } else if ("authPriv".equalsIgnoreCase(securityLevel)) {
                        snmp = SnmpClient.create("3")
                                .setHost(ip)
                                .setPort(prot)
                                .setSerurityName(serurityName)
                                .setAuthenticationProtocol(authenticationProtocol)
                                .setAuthenticationPassphrase(authenticationPassword)
                                .setPrivacyProtocol(encryptionProtocol)
                                .setPrivacyPassphrase(encryptionPassword)
                                .setSecurityLevel(securityLevel);
                    } else if ("noAuthNoPriv".equalsIgnoreCase(securityLevel)) {
                        snmp = SnmpClient.create("3")
                                .setHost(ip)
                                .setPort(prot)
                                .setSerurityName(serurityName)
                                .setSecurityLevel(securityLevel);
                    }

                    try {
                        if (snmp != null) {
                            snmp.connect();
                            List<IfEntry> list = snmp.walkIfTable(ip, scId);
                            if (list != null && list.size() != 0) {
                                //保存ifentry
//                                netWorkFindDao.ifentry(list);
                                Map map = snmp.walkIpAddrTable(ip, scId);
                                //保存ipNetToMediaPhysAddress
                                List ipNetToMediaPhysAddressList = (List) map.get("ipNetToMediaPhysAddressList");
                                if (ipNetToMediaPhysAddressList != null && ipNetToMediaPhysAddressList.size() != 0) {
//                                    netWorkFindDao.ipNetToMediaPhysAddressSave(ipNetToMediaPhysAddressList);
                                }
                                List ipNetToMediaTypeList = (List) map.get("ipNetToMediaTypeList");
                                if (ipNetToMediaTypeList != null && ipNetToMediaTypeList.size() != 0) {
//                                    netWorkFindDao.ipNetToMediaTypeSave(ipNetToMediaTypeList);
                                }
                                List ipAddrTableList = (List) map.get("ipAddrTableList");
                                if (ipAddrTableList != null && ipAddrTableList.size() != 0) {
//                                    netWorkFindDao.ipAddrTableSave(ipAddrTableList);
                                }
                                break;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        snmp.close();
                    }
                }
            }
        }
    }
}
